var eventVm = new Vue({
	el: "#event-record-wrapper",
	data: function() {
		return {
			equipmentid: '',
			departid: '',
			depart: '',
			departError: '',
			workshopid: '',
			workshop: '',
			workshopSourceData: [],
			workshopError: '',
			eventType: '',
			eventTypeError: '',
			finishDate: '',
			finishDateError: '',
			desc: '',
			descError: '',
			zcbh: '',
			tmbh: '',
			type: '',
			name: '',
			model: ''
		}
	},
	mounted: function (){
		var self = this;
		mui.init();
		mui.plusReady(function () {
		    self.initLayout();
			self.initValidator();
			self.initDapertData();
			self.initWorkshopData();
			self.initEventTypeData();
		})
	},
	methods:{
		initDapertData: function() {
			var self = this;
			this.departPicker = new mui.PopPicker();
			var url = config.requestBaseUrl + 'getSystemStandData';
			mui.ajax(url,{
			 	data:{
					type:'depart'
			 	},
			 	dataType:'json',//服务器返回json格式数据
			 	type:'get',//HTTP请求类型
			 	timeout:3000,//超时时间设置为10秒；
			 	success:function(data){
					var departData = [];
					data.data.forEach(function(item, index) {
						departData.push({value: item.id, text: item.name})
					})
					self.departPicker.setData(departData);
			 	},
			 	error:function(xhr,type,errorThrown){
					mui.toast("出错了:" + errorThrown);
			 	}
			});
		},
		initWorkshopData: function() {
			var self = this;
			this.workshopPicker = new mui.PopPicker();
			var url = config.requestBaseUrl + 'getSystemStandData';
			mui.ajax(url,{
			 	data:{
					type: 'workshop'
			 	},
			 	dataType:'json',//服务器返回json格式数据
			 	type:'get',//HTTP请求类型
			 	timeout:3000,//超时时间设置为10秒；
			 	success:function(data){
					self.workshopSourceData = data.data;
			 	},
			 	error:function(xhr,type,errorThrown){
					mui.toast("出错了:" + errorThrown);
			 	}
			});
		},
		initEventTypeData: function() {
			var self = this;
			this.eventTypePicker = new mui.PopPicker();
			var url = config.requestBaseUrl + 'getSystemStandData';
			mui.ajax(url,{
			 	data:{
					type:'eventType'
			 	},
			 	dataType:'json',//服务器返回json格式数据
			 	type:'get',//HTTP请求类型
			 	timeout:3000,//超时时间设置为10秒；
			 	success:function(data){
					var pickerData = [];
					if(data.isSuccess) {
						data.data.forEach(function(item, index) {
							pickerData.push({value: item.code, text: item.name})
						})
					}
					self.eventTypePicker.setData(pickerData);
			 	},
			 	error:function(xhr,type,errorThrown){
					mui.toast("出错了:" + errorThrown);
			 	}
			});
		},
		initValidator: function() {
			this.constraints = {
				depart: {
					presence: true
				},
				eventType: {
					presence: true,
					length: {
						maximum: 12,
						message: "长度不能超过12"
					}
				},
				finishDate: {
					presence: true,
					length: {
						maximum: 12,
						message: "长度不能超过12"
					}
				},
				desc: {
					length: {
						maximum: 60,
						message: "长度不能超过60"
					}
				}
			};
			// 为空时的信息
			validate.validators.presence.message = "不能为空!";
		},
		toDepartPicker: function() {
			var self = this;
			this.clearErrorInfo('depart');
			this.departPicker.show(function (selectItems) {
				self.depart = selectItems[0].text;
				self.departid = selectItems[0].value;
				setTimeout(function() {
					self.workshopDataConst();
				}, 10);
			})
		},
		toWorkshopPicker:function() {
			var self = this;
			if(!this.depart) {
				mui.toast("请先选择部门!");
			} else {
			this.clearErrorInfo('workshop');
			this.workshopPicker.show(function (selectItems) {
				self.workshop = selectItems[0].text;
				self.workshopid = selectItems[0].value;
			})
			}
		},
		toEventTypePicker: function() {
			var self = this;
			this.clearErrorInfo('type');
			this.eventTypePicker.show(function (selectItems) {
				self.eventType = selectItems[0].text;
			})
		},
		workshopDataConst: function() {
			var self = this;
			var workshopData = [];
			this.workshopSourceData.forEach(function(item, index) {
				if(item.id == self.departid) {
					workshopData.push({value: item.id, text: item.name});
				}
			})
			this.workshopPicker.setData(workshopData);
		},
		clearErrorInfo: function(filed) {
			this[ filed+ 'Error'] = '';
		},
		initLayout:function() {
			mui('.mui-scroll-wrapper').scroll({
				scrollY: true, //是否竖向滚动
				scrollX: false, //是否横向滚动
				startX: 0, //初始化时滚动至x
				startY: 0, //初始化时滚动至y
				indicators: true, //是否显示滚动条
				deceleration: 0.0006, //阻尼系数,系数越小滑动越灵敏
				bounce: true //是否启用回弹
			})
		},
		openBarCodePage: function() {
			createWithoutTitle('barcode_scan.html', {
				titleNView:{
					type: 'float',
					backgroundColor: 'rgba(215,75,40,0.3)',
					titleText: '扫一扫',
					titleColor: '#FFFFFF',
					autoBackButton: true,
					buttons: [{
						fontSrc: '_www/helloh5.ttf',
						text: '\ue302',
						fontSize: '18px',
						onclick: 'javascript:scanPicture()'
					}]
				}
			});
		},
		scaned: function(t, r, f){
			if(t == "CODE128") {
				this.tmbh = r;
				this.tmbhError = false;
			} else {
				this.zcbh = r;
				this.zcbhError = false;
			}
			this.getEquipmentInfo();
		},
		getEquipmentInfo: function() {
			var self = this;
			var url = config.requestBaseUrl + 'getEquipmentBaseInfoByBH';
			
			mui.ajax(url,{
				data:{
					zcbh: this.zcbh,
					tmbh: this.tmbh,
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:10000,//超时时间设置为10秒；
				success:function(data){
					console.log(data.msg + data.isSuccess);
					if(data.isSuccess) {
						self.equipmentid = data.data.id;
						self.zcbh = data.data.zcbh;
						self.tmbh = data.data.tmbh;
						self.type = data.data.type;
						self.name = data.data.name;
						self.model = data.data.model;
					} else {
						mui.toast(data.msg);
					}
				},
				error:function(xhr,type,errorThrown){
					mui.toast("出错啦:" + errorThrown);
				}
			});
		},
		submitEvent: function() {
			var data = {};
			var self = this;
			if(this.depart) {
				data['depart'] = this.depart;
			}
			if(this.eventType) {
				data['eventType'] = this.eventType;
			}
			if(this.finishDate) {
				data['finishDate'] = this.finishDate;
			}
			if(this.desc) {
				data['desc'] = this.desc;
			}
			var errors = validate(data, this.constraints);
			
			if(errors) {
				if(errors.depart) {
					this.departError = errors.depart;
				}
				if(errors.eventType) {
					this.eventTypeError = errors.eventType;
				}
				if(errors.finishDate) {
					// this.finishDateError = errors.finishDate;
					mui.toast("预计完成时间不能为空!");
				}
				if(errors.desc) {
					this.descError = errors.desc;
				}
			} else {
				this.submitData();
			}
		},
		submitData: function() {
			var self = this;
			var selfPage = plus.webview.currentWebview();
			var recorderid = plus.storage.getItem("userid");
			var url = config.requestBaseUrl + 'addEvent';
			mui.ajax(url,{
			 	data:{
			 		depart: this.depart,
					departid: this.departid,
					workshop: this.workshop,
					workshopid: this.workshopid,
					type: this.eventType,
					finishDate: this.finishDate,
					desc: this.desc,
					equipmentid: this.equipmentid,
					recorderid: recorderid
			 	},
			 	dataType:'json',//服务器返回json格式数据
			 	type:'post',//HTTP请求类型
			 	timeout:10000,//超时时间设置为10秒；
			 	success:function(data){
			 		mui.toast(data.msg);
					if(data.isSuccess) {
						var indexPage = plus.webview.getLaunchWebview();
						mui.fire(indexPage,'refresh',{});
						selfPage.close()
					}
			 	},
			 	error:function(xhr,type,errorThrown){
			 		mui.toast("出错啦:" + errorThrown)
			 	}
			 });
		 }
	}
})

function scaned(t, r, f){
	eventVm.scaned(t, r, f);
}